package com.cx.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cx.mapper.LolBattleChampionMapper;
import com.cx.po.LolBattleChampionPo;
import com.cx.po.LolBattleTeamPo;
import com.cx.service.LolBattleChampionService;
import com.cx.vo.LolBattleChampionVo;
import com.cx.vo.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 残星
 * @Description LOL-对局英雄信息 业务逻辑类
 * @date 2022-11-30
 */
@Service
public class LolBattleChampionServiceImpl extends ServiceImpl<LolBattleChampionMapper, LolBattleChampionPo> implements LolBattleChampionService {
    @Resource(name = "mongoTemplate")
    private MongoTemplate mongoTemplate;
    @Resource(name = "lolBattleChampionMapper")
    private LolBattleChampionMapper lolBattleChampionMapper;

    @Override
    public Page<LolBattleChampionPo> findLolChampionPage(Page<LolBattleChampionPo> page) {
        return lolBattleChampionMapper.findLolChampionPage(page);
    }

    @Override
    public ResultVo<Object> updateLolBattleChampionDocument(LolBattleChampionVo lolBattleChampionVo) {
        ResultVo<Object> resultVo = new ResultVo<>();
        if (StrUtil.isEmpty(lolBattleChampionVo.getId())) {
            resultVo.result("500", "MongoDB主键不能为空！");
            return resultVo;
        }
        LolBattleChampionPo lolBattleChampionPo = new LolBattleChampionPo();
        BeanUtils.copyProperties(lolBattleChampionVo, lolBattleChampionPo);
        try {
            mongoTemplate.save(lolBattleChampionPo);
            resultVo.setSuccess(true);
            resultVo.result("0", "修改对局记录成功！");
        } catch (Exception e) {
            e.printStackTrace();
            resultVo.result("500", "修改对局记录失败！");
        }
        return resultVo;
    }

    @Override
    public ResultVo<Object> deleteLolBattleChampion(Long gameId) {
        ResultVo<Object> resultVo = new ResultVo<>();
        if (gameId == null || gameId == 0L) {
            resultVo.result("500", "对局ID不能为空！");
            return resultVo;
        }
        try {
            Query queryIdList = Query.query(Criteria.where("gameId").is(gameId));
            List<LolBattleChampionPo> championPoList = mongoTemplate.findAllAndRemove(queryIdList, LolBattleChampionPo.class);
            resultVo.setSuccess(true);
            resultVo.result("0", "删除对局英雄记录成功！", championPoList);
        } catch (Exception e) {
            e.printStackTrace();
            resultVo.result("500", "删除对局英雄记录失败！");
        }
        return resultVo;
    }

    @Override
    public ResultVo<Object> deleteLolBattleChampionByTeam(Long gameId, String team) {
        ResultVo<Object> resultVo = new ResultVo<>();
        if (gameId == null || gameId == 0L || StrUtil.isEmpty(team)) {
            resultVo.result("500", "对局ID和队伍不能为空！");
            return resultVo;
        }
        try {
            Query queryIdList = Query.query(Criteria.where("gameId").is(gameId).and("team").is(team));
            List<LolBattleChampionPo> championPoList = mongoTemplate.findAllAndRemove(queryIdList, LolBattleChampionPo.class);
            resultVo.setSuccess(true);
            resultVo.result("0", "删除对局英雄记录成功！", championPoList);
        } catch (Exception e) {
            e.printStackTrace();
            resultVo.result("500", "删除对局英雄记录失败！");
        }
        return resultVo;
    }

    @Override
    public ResultVo<Object> deleteLolBattleChampion(Long gameId, String id) {
        ResultVo<Object> resultVo = new ResultVo<>();
        if (gameId == null || gameId == 0L || StrUtil.isEmpty(id)) {
            resultVo.result("500", "对局ID不能为空！");
            return resultVo;
        }
        try {
            Query queryIdList = Query.query(Criteria.where("gameId").is(gameId).and("id").is(id));
            List<LolBattleChampionPo> championPoList = mongoTemplate.findAllAndRemove(queryIdList, LolBattleChampionPo.class);
            resultVo.setSuccess(true);
            resultVo.result("0", "删除对局英雄记录成功！", championPoList);
        } catch (Exception e) {
            e.printStackTrace();
            resultVo.result("500", "删除对局英雄记录失败！");
        }
        return resultVo;
    }
}